package fr.utt.lo02.CrazyEights.view;



import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JLabel;

import fr.utt.lo02.CrazyEights.model.cards.Card;
import fr.utt.lo02.CrazyEights.model.cards.CardRank;
import fr.utt.lo02.CrazyEights.model.cards.CardSuit;


/**
 * Represents an image in a label
 * @author Jeremy
 *
 */
public class ImageLabel extends JLabel{

	private static final long serialVersionUID = 1L;
	/**
	 * The image path
	 */
	private String imagePath;
	/**
	 * Card represented by the label
	 */
	private Card card;
	
	/**
	 * Create the label with the given image path
	 * @param path the image path
	 */
	public ImageLabel(String path){
		this.imagePath = path;
	}
	
	/**
	 * Create the label with the path of the cards back
	 */
	public ImageLabel(){
		this.imagePath = "resources/back.png";
	}
	
	/**
	 * Create the label for the given card
	 * @param card the card for which we create the label
	 */
	public ImageLabel(Card card){
		this.card = card;
		this.imagePath = this.getCardPath(card);
	}
	
	public Card getCard(){
		return card;
	}
	
	/**
	 * 
	 * @param card the card for which we search the path
	 * @return the path of the given card
	 */
	public String getCardPath(Card card){
		String path="resources/";
		if(card.getRank()==CardRank.Joker){
			path+="joker/";
			if(card.getSuit()==CardSuit.BLACK){
				path+="black.png";
			}
			else{
				path+="red.png";
			}
		}
		else{
			switch(card.getSuit()){
			case CLUBS:
				path+="club/";
				break;
			case DIAMONDS:
				path+="diamond/";
				break;
			case HEARTS:
				path+="heart/";
				break;
			case SPADES:
				path+="spade/";
			}
			switch(card.getRank()){
			case Ace:
				path+="ace.png";
				break;
			case Two:
				path+="two.png";
				break;
			case Three:
				path+="three.png";
				break;
			case Four:
				path+="four.png";
				break;
			case Five:
				path+="five.png";
				break;
			case Six:
				path+="six.png";
				break;
			case Seven:
				path+="seven.png";
				break;
			case Eight:
				path+="eight.png";
				break;
			case Nine:
				path+="nine.png";
				break;
			case Ten:
				path+="ten.png";
				break;
			case Jack:
				path+="jack.png";
				break;
			case Queen:
				path+="queen.png";
				break;
			case King:
				path+="king.png";
				break;
			}
		}
		return path;
	}
	
	/**
	 * Display the imageLabel
	 */
	public void paint(Graphics g){
		Dimension d = getSize();
			try {
				Image img = ImageIO.read(new File(imagePath));
				g.drawImage(img, 0, 0, d.width, d.height, this);
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	
	public void setImagePath(String path){
		this.imagePath = path;
		repaint();
	}
	
	public void setImagePath(Card card){
		this.imagePath = this.getCardPath(card);
		repaint();
	}
}